<?php
class Common_Service_Currency
{

    public static function add($row){
        $obj = new Common_Model_Currency();
        return $obj->add($row);
	}
 
	public static function getByCode($code){
        $obj = new Common_Model_Currency();
        return $obj->getByCode($code);
	}
	/*
	*币种与金额转换
	*$value 转换金额
	*$orgCode 原始货币CODE
	*$disCode 目的货币CODE
	*/
	public static function changeCurrency($value,$orgCode,$disCode,$orgRate=null){
	
		 
		$orgCode = strtoupper($orgCode);
		$disCode = strtoupper($disCode);
		$obj = new Common_Model_Currency();	
		if(isset($orgRate)&$orgRate!=0){
			$orgCurrency['currency_rate'] = $orgRate;
		}else{
			$orgCurrency = $obj->getByCode($orgCode);
		}
		if($orgCode==$disCode){
			$result['ask'] = 'Success';
			$result['value'] = $value;
			
			$result['currency_code'] = $disCode;
			$result['org_rate'] = $orgCurrency['currency_rate'];
			$result['dis_rate'] = $orgCurrency['currency_rate'];
			$result['rate'] = 1;
			return $result;
		}
		
        
		$disCurrency = $obj->getByCode($disCode);
		 
		if(!$orgCurrency||!$disCurrency){
			$result = array('ask'=>'Error','message'=>'Can not find origin/destination currency code');
		}else{
			//取小数后两位，不作四舍五入
			$result['ask'] = 'Success';
			$result['org_rate'] = $orgCurrency['currency_rate'];
			$result['dis_rate'] = $disCurrency['currency_rate'];
			$result['rate'] = sprintf("%.5f",$orgCurrency['currency_rate']/$disCurrency['currency_rate']);
			$result['value'] = sprintf("%.2f",$value*$result['rate']);
			$result['currency_code'] = $disCode;
		}
        return $result;
	}
	
		public static function create($row){
			$obj = new Common_Model_Currency();
			return $obj->create($row);
		}

		public static function get($id){
			$obj = new Common_Model_Currency();
			return $obj->get($id);
		}

		public static function getByValue($value,$field='currency_id'){
			$obj = new Common_Model_Currency();
			return $obj->getByValue($value,$field);
		}
		
		public static function update($row,$id){
			$obj = new Common_Model_Currency();
			return $obj->update($row,$id);
		}

		public static function delete($id){
			$obj = new Common_Model_Currency();
			return $obj->delete($id);
		}

		public static function getByCondition($condition=array(),$type='*', $pageSize=0, $page=1,$order=array('currency_id')){
			$obj = new Common_Model_Currency();
			return $obj->getByCondition($condition,$type,$pageSize,$page,$order);
		}

		public static function validator($val){
			$error = array();
			$validateArr = array();
			$validateArr[] = array('name' => '货币名称', 'value' => $val['currency_name'], 'regex' => array('require'));
			$validateArr[] = array('name' => '中文名', 'value' => $val['currency_cn_name'], 'regex' => array('require', 'chinese', ));
			$validateArr[] = array('name' => '简称', 'value' => $val['currency_code'], 'regex' => array('require'));
			$validateArr[] = array('name' => '表示符号左', 'value' => $val['currency_symbol_left'], 'regex' => array('require', 'length[0,255]', ));
			$validateArr[] = array('name' => '费率', 'value' => $val['currency_rate'], 'regex' => array('require', 'positive1', ));
			$error = Common_Service_Validator::formValidator($validateArr);
			return $error;
		}
		
		public static function getAll(){
			$obj = new Common_Model_Currency();
			return $obj->getAll();
		}
		
 
}